package exp4;
import exp3.userdata;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet( urlPatterns = {"/kk1"})

public class login_exp4 extends HttpServlet {
    @Override
   /* protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String id=req.getParameter("username");
        String pwd=req.getParameter("password");
        System.out.println(id);
        System.out.println(pwd);
        Connection conn = (Connection) getServletContext().getAttribute("connection");
        try (
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("select id password from userdata");

        ) {
            while (rs.next()) {
                System.out.println(rs.getString("id"));
                System.out.println(rs.getString("password"));
                if(rs.getString("id").equals(id)&&rs.getString("password").equals(pwd)){
                    userdata person=new userdata(id,rs.getString("username"),pwd,rs.getString("email"), rs.getString("job"));
                    System.out.println(person.getUsername()+person.getJob());
                    HttpSession session = req.getSession();
                    session.setAttribute("user",person);
                    resp.sendRedirect("http://localhost:8080/loginss.jsp");
                }
            }
//            PrintWriter out = resp.getWriter();
//            out.print("<script>alert(\"用户名或者密码错误\");window.location.href='http://localhost:8080/html/jiemian.html'</script>");
            resp.sendRedirect("/error_login_exp3.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }*/
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userName=new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
        String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("org.h2.jdbcx.JdbcDataSource");
                /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
                String url="jdbc:h2:~/first";
                con=DriverManager.getConnection(url,"sa","sa");
                stmt=con.createStatement();
//                String sql="select * from user where userName='"+userName+"'";
                String sql="select * from userdata ";
                rs=stmt.executeQuery(sql);
                while(rs.next()) {
                    System.out.println("sss");

                     if(userName.equals(rs.getString("id"))&&password.equals((rs.getString("password"))))
                    {
                        HttpSession session = request.getSession();//获取session对象
                        PrintWriter out = response.getWriter();
                        userdata person=new userdata(userName,rs.getString("username"),password,rs.getString("email"), rs.getString("job"));
                        request.getSession().setAttribute("user",person);
                        response.sendRedirect("http://localhost:8080/loginss.jsp");
                    }
                    PrintWriter out = response.getWriter();
                    out.print("<script>alert(\"password or username mistake\")</script>");

                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
